Design system.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

255 lines
7.4 KiB

  1. import type {GetStaticPaths, GetStaticProps, NextPage} from 'next';
  2. import * as fs from 'fs/promises';
  3. import * as path from 'path';
  4. import * as React from 'react'
  5. import {useRouter} from 'next/router';
  6. import ReactMarkdown from 'react-markdown';
  7. import {DocsLayout, Page} from '@/components/DocsLayout';
  8. import {getDocs, getPlatforms} from '@/utils/data';
  9. export interface Props {
  10. componentPages: { name: string; components: Page[] }[],
  11. docsPages: Page[],
  12. examplePages: Page[],
  13. markdown: string,
  14. platforms: string,
  15. platform: string,
  16. framework: string,
  17. }
  18. const InnerPage: NextPage<Props> = ({
  19. componentPages,
  20. examplePages,
  21. docsPages,
  22. markdown,
  23. platforms,
  24. platform,
  25. framework,
  26. }) => {
  27. const router = useRouter();
  28. const sidebarOpen = router.query.open === 'sidebar';
  29. return (
  30. <DocsLayout
  31. componentPages={componentPages}
  32. examplePages={examplePages}
  33. docsPages={docsPages}
  34. platforms={platforms}
  35. currentPlatform={framework}
  36. currentVersion="0.1.0"
  37. sidebarOpen={sidebarOpen}
  38. >
  39. {markdown && (
  40. <ReactMarkdown
  41. className="my-12 leading-loose"
  42. components={{
  43. ul: ({node, ordered: _ordered, ...props}) => (
  44. <ul
  45. {...props}
  46. className="list-disc pl-4"
  47. />
  48. ),
  49. li: ({node, ...props}) => (
  50. <li
  51. {...props}
  52. className="list-item pl-4"
  53. />
  54. ),
  55. }}
  56. >
  57. {markdown}
  58. </ReactMarkdown>
  59. )}
  60. <pre>
  61. <code>
  62. {JSON.stringify(componentPages, null, 2)}
  63. </code>
  64. </pre>
  65. </DocsLayout>
  66. )
  67. }
  68. export const getStaticProps: GetStaticProps = async (ctx) => {
  69. const { params } = ctx;
  70. const { url } = params as { url: string[] };
  71. const [platform, framework, ...etcUrl] = url;
  72. const props = {} as Record<string, unknown>;
  73. props.platform = platform;
  74. props.framework = framework;
  75. props.docsPages = await getDocs({
  76. deriveHrefFromFilename: d => `/docs/${
  77. d
  78. .replace(/\.md/i, '')
  79. .split('-')
  80. .slice(1)
  81. .join('-')
  82. }`,
  83. deriveLabelFromFilename: d => (
  84. d
  85. .split('-')
  86. .slice(1)
  87. .map((dd) => dd.slice(0, 1).toUpperCase() + dd.slice(1))
  88. .join(' ')
  89. .replace(/\.md/i, '')
  90. ),
  91. });
  92. props.platforms = await getPlatforms();
  93. // const categoriesPath = path.resolve('../../categories');
  94. // const categories = await fs.readdir(categoriesPath);
  95. // props.componentPages = categories.map((c) => ({
  96. // id: c,
  97. // href: `/categories/${c}`,
  98. // label: c.split('-').map((cc) => cc.slice(0, 1).toUpperCase() + cc.slice(1)).join(' '),
  99. // }));
  100. // const pagesPath = path.resolve('src/pages');
  101. // const examplesPath = path.resolve(pagesPath, 'examples');
  102. // const examplesRaw = await fs.readdir(examplesPath);
  103. // const examplesIndexPage = await Promise.all(
  104. // examplesRaw.map(async (c) => {
  105. // const indexPath = await path.resolve(examplesPath, c, 'index.tsx');
  106. // try {
  107. // const statResult = await fs.stat(indexPath);
  108. // return [c, statResult.isFile()];
  109. // } catch {
  110. // // noop
  111. // }
  112. //
  113. // return [c, false];
  114. // })
  115. // ) as [string, boolean][];
  116. // const examples = examplesIndexPage
  117. // .filter(([, hasIndexPage]) => hasIndexPage)
  118. // .map(([key]) => key);
  119. // props.examplePages = examples.map((e) => ({
  120. // id: e,
  121. // href: `/examples/${e}`,
  122. // label: e.split('-').map((ee) => ee.slice(0, 1).toUpperCase() + ee.slice(1)).join(' '),
  123. // }));
  124. // const typedocData = await fs.readFile('typedoc.data.json', 'utf-8');
  125. // const project = JSON.parse(typedocData) as any;
  126. //
  127. // props.componentPages = project.children.reduce(
  128. // (theComponents, pkg) => {
  129. // const packageNameFragments = pkg.name.split('-');
  130. // const packageFramework = packageNameFragments.pop();
  131. // const categoryBaseName = packageNameFragments.pop();
  132. //
  133. // console.log(packageFramework, categoryBaseName);
  134. //
  135. // if (theComponents.some((c) => c.name === categoryBaseName)) {
  136. // return theComponents.map((cc) => {
  137. // if (cc.name !== categoryBaseName) {
  138. // return cc;
  139. // }
  140. //
  141. // let components = [];
  142. // if (packageFramework === 'react') {
  143. // components = pkg.children
  144. // .filter((exported) => {
  145. // return exported.kind === 64; // Function, these are react components
  146. // })
  147. // .map((component) => {
  148. // return {
  149. // ...component,
  150. // id: component.name,
  151. // href: `/categories/${categoryBaseName}/${component.name}`,
  152. // label: component.name,
  153. // descriptionMarkdown: component.signatures[0].comment.summary.reduce(
  154. // (theText, t) => {
  155. // if (t.kind === 'text') {
  156. // return `${theText}${t.text}`;
  157. // }
  158. // if (t.kind === 'inline-tag' && t.tag === '@link') {
  159. // return `${theText}[${t.text}](#)` // TODO set URL
  160. // }
  161. // return theText;
  162. // },
  163. // ''
  164. // ),
  165. // };
  166. // });
  167. // }
  168. //
  169. // return {
  170. // ...cc,
  171. // name: categoryBaseName,
  172. // packages: {
  173. // ...(cc.packages ?? {}),
  174. // [packageFramework]: {
  175. // components,
  176. // },
  177. // },
  178. // };
  179. // })
  180. // }
  181. //
  182. // let components = [];
  183. // if (packageFramework === 'react') {
  184. // components = pkg.children
  185. // .filter((exported) => {
  186. // return exported.kind === 64; // Function, these are react components
  187. // })
  188. // .map((component) => {
  189. // return {
  190. // ...component,
  191. // id: component.name,
  192. // href: `/categories/${categoryBaseName}/${component.name}`,
  193. // label: component.name,
  194. // descriptionMarkdown: component.signatures[0].comment.summary.reduce(
  195. // (theText, t) => {
  196. // if (t.kind === 'text') {
  197. // return `${theText}${t.text}`;
  198. // }
  199. // if (t.kind === 'inline-tag' && t.tag === '@link') {
  200. // return `${theText}[${t.text}](#)` // TODO set URL
  201. // }
  202. // return theText;
  203. // },
  204. // ''
  205. // ),
  206. // };
  207. // });
  208. // }
  209. //
  210. // return [
  211. // ...theComponents,
  212. // {
  213. // name: categoryBaseName,
  214. // packages: {
  215. // [packageFramework]: {
  216. // components,
  217. // }
  218. // }
  219. // }
  220. // ]
  221. // },
  222. // [],
  223. // );
  224. return {
  225. props,
  226. };
  227. };
  228. export default InnerPage;
  229. export const getStaticPaths: GetStaticPaths = async () => {
  230. const docsPath = path.resolve('../../docs');
  231. const docs = await fs.readdir(docsPath);
  232. const categoriesPath = path.resolve('../../categories');
  233. const categories = await fs.readdir(categoriesPath);
  234. return {
  235. paths: [
  236. ...docs.map((d) => `/docs/${d}`),
  237. ],
  238. fallback: true,
  239. };
  240. };